//备注：拷贝代码请加上作者信息
//作者：王海涛
//邮箱：1126471088@qq.com
//版本：V0.2.2.1
#include "debug_driver.h"

static WHT_UART_Cache_t wht_cache;

static struct
{
    uint8_t lock;
    uint8_t length;
    uint8_t buffer[DEBUG_Max_Buff_Count];
}WHT_Rx_Buffer[DEBUG_Max_Queue_Count];



/*Debug发送空闲中断回调函数*/
static void WHT_Debug_Driver_Tx_Idle_IT_Callback(void)
{
    //不用了
}
/*Debug接收空闲中断回调函数*/
static void WHT_Debug_Driver_Rx_Idle_IT_Callback(void)
{
    static uint8_t Pos = 0;

    if (WHT_Rx_Buffer[Pos].lock == DISABLE)
    {
        WHT_Rx_Buffer[Pos].length = wht_cache.Rx_Count;
        memcpy(WHT_Rx_Buffer[Pos].buffer, wht_cache.Rx_Buf, WHT_Rx_Buffer[Pos].length);
        WHT_Rx_Buffer[Pos].lock = ENABLE;
        Pos = (Pos + 1) % (sizeof(WHT_Rx_Buffer) / sizeof(WHT_Rx_Buffer[0]));
    }
    wht_cache.Rx_Count = 0;
    WHT_UART_BSP.WHT_Interrupt_Receive(&wht_cache);
}
/*Debug发送FIFO数据*/
static void WHT_Debug_Driver_Send_Buf(const uint8_t* tx_buf, uint32_t send_length)
{
    //等待发送完成
    while (wht_cache.Info->Tx_State != UART_Tx_Idle)
        WHT_Delay_ms(2);
    wht_cache.Tx_Count = send_length;
    wht_cache.Tx_Buf = (uint8_t*)tx_buf;
    //WHT_UART_BSP.WHT_DMA_Send(&wht_cache);
    WHT_UART_BSP.WHT_Interrupt_Send(&wht_cache);
}




/*Debug发送数据*/
static void WHT_Printf(const char* tx_buf, uint16_t send_length)
{
    WHT_Debug_Driver_Send_Buf((const uint8_t*)tx_buf, send_length);
}
/*Debug获取接收状态和数据*/
static uint16_t WHT_Scanf(uint8_t* rx_buf, uint16_t max_receive_length, uint32_t timeout)
{
    static uint8_t Pos = 0;
    uint16_t receive_count = 0;

    if (WHT_Rx_Buffer[Pos].lock == ENABLE)
    {
        receive_count = max_receive_length > WHT_Rx_Buffer[Pos].length ? WHT_Rx_Buffer[Pos].length : max_receive_length;
        memcpy(rx_buf, WHT_Rx_Buffer[Pos].buffer, receive_count);
        WHT_Rx_Buffer[Pos].lock = DISABLE;
        Pos = (Pos + 1) % (sizeof(WHT_Rx_Buffer) / sizeof(WHT_Rx_Buffer[0]));
    }
    return receive_count;
}
/*Debug初始化注册*/
static void WHT_Debug_Driver_Register(uint32_t BaudRate)
{
    static uint8_t Temp_Buffer[sizeof(WHT_Rx_Buffer[0].buffer)];

    wht_cache.Rx_Buf_Size = sizeof(Temp_Buffer);
    wht_cache.Rx_Count = 0;
    wht_cache.Rx_Buf = Temp_Buffer;
    wht_cache.Rx_Idle_IT_Callback = WHT_Debug_Driver_Rx_Idle_IT_Callback;
    wht_cache.Tx_Idle_IT_Callback = WHT_Debug_Driver_Tx_Idle_IT_Callback;

    WHT_UART_Config_t wht_config = WHT_UART_DEFAULT_CONFIG(UART_1);
    wht_config.Bps = BaudRate;
    WHT_UART_BSP.WHT_Register(&wht_config, &wht_cache);
    WHT_UART_BSP.WHT_Config(&wht_config, &wht_cache);
    WHT_UART_BSP.WHT_Interrupt_Receive(&wht_cache);
}


const WHT_Debug_Driver_t WHT_Debug_Driver =
{
    .WHT_Init = WHT_Debug_Driver_Register,
    .WHT_Printf = WHT_Printf,
    .WHT_Scanf = WHT_Scanf,
};





/*Debug阻塞发送*/
static void WHT_Block_Printf(const char* tx_buf, uint32_t send_length)
{
    //等待发送完成
    while (wht_cache.Info->Tx_State != UART_Tx_Idle);
    wht_cache.Tx_Count = send_length;
    wht_cache.Tx_Buf = (uint8_t*)tx_buf;
    WHT_UART_BSP.WHT_Send(&wht_cache);
}

//重定向c库函数printf到串口，重定向后可使用printf函数
int fputc(int ch, FILE* f)
{
    WHT_Block_Printf((char*)&ch, 1);
    return ch;
}
//重定向c库函数scanf到串口，重写向后可使用scanf、getchar等函数
int fgetc(FILE* f)
{
    uint8_t rx_buf[1];

    WHT_Scanf(rx_buf, 1, 1);
    return (int)rx_buf[0];
}
